<?php

namespace DB;

use Config\Base as Config;
use Exception;
use Facebook;
use Models\Album;
use Models\Photo;
use Models\Profile;

class FacebookService {
    /**
     * @var \Facebook
     */
    private static $api = null;

    public function __construct() {
        if (!self::$api) {
            $cfg = Config::instance()->facebook;
            self::$api = new Facebook(array(
                'appId' => $cfg->APP_ID,
                'secret' => $cfg->APP_SECRET
            ));
        }
    }

    private function query($fql) {
        return self::$api->api(array(
            'method' => 'fql.query',
            'query' => $fql
        ));
    }

    /**
     * @return integer
     */
    public function getUser() {
        $user_id = self::$api->getUser();
        return $user_id;
    }

    public function getLoginUrl(array $param) {
        return self::$api->getLoginUrl($param);
    }

    public function getLogoutUrl(array $param) {
        return self::$api->getLogoutUrl($param);
    }

    /**
     * @return Profile
     */
    public function getProfile() {
        $user = \App::getCurrentUser();
        $fql = 'SELECT uid,
			name, first_name, last_name, 
			sex, timezone, username, 
			email, profile_url, locale, 
			pic_small_with_logo, pic_big_with_logo, pic_square_with_logo, pic_with_logo, 
			sports 
			FROM user WHERE uid = me()';
        $profile = array_shift($this->query($fql));
        $profile = new Profile(array(
            'id' => $profile['uid'],
            'provider' => Profile::PROVIDER_FB,
            'data' => $profile,
            'access_token' => '', //TODO: fix
        ));

        if ($user) {
            $profile->setUser($user);
        }

        return $profile;
    }

    /**
     * @return Album[]
     */
    public function getPageAlbums() {
        $pid = Config::instance()->facebook->PAGE_ID;
        $fql = "SELECT
			aid, owner, name, object_id, link, photo_count, created, modified
			FROM album 
			WHERE owner={$pid}";

        $albums = $this->query($fql);
        foreach ($albums as &$data) {
            $data['updated'] = $data['modified'];
            $data = new Album($data);
        }

        return $albums;
    }

    /**
     * @param int $object_id
     * @throws Exception
     * @return Album
     */
    public function getAlbum($object_id) {
        $fql = "SELECT
			aid, owner, name, object_id, link, photo_count, created, modified, cover_object_id
			FROM album 
			WHERE object_id={$object_id}";

        $album = $this->query($fql);

        if (!count($album)) {
            throw new Exception("Album [{$object_id}] not found on Facebook");
        }
        $album = array_shift($album);
        $album['updated'] = $album['modified'];
        $album = new Album($album);

        return $album;
    }

    /**
     * @param int $album_id
     * @return Photo[]
     */
    public function getAlbumPhotos($album_id) {
        $fql = "SELECT
			object_id,
			position,
			owner,
			caption,
			link,
			images,
			album_object_id
			FROM photo 
			WHERE album_object_id = {$album_id}";

        $photos = $this->query($fql);

        foreach ($photos as &$p) {
            $p = new Photo($p);
        }

        return $photos;
    }


}